package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000121648961";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCQCghBWJn9lCSmw237zy3bn7qtHFyJaz45Iz++peBg6yqsXgQkDAogQA2x83zvB3FNiBD1VszIKzCoq5hmHoHtFXirqPnEOzNSlgIzOK0CwFNV7+ibVp2dp9rYMDP08E3hzeHOixfsLzFtRUylUbmwVv/dLIXvC+FDwavhUTHv27yUn6LbWqewGrHqmv12+IsSCsgxXl3f9d7931DhIgEmFS7FQmmv7I3bYBNd7Ca7oF2DOiRaLCCmL1jXpaLjJcPUZZrl2wNTcApTwRm4E7lEpF352zvFN1DYLCGOORxLCdV27rAy7ciPCHTg30mxNYrjkyG7SytWQwUXKhQ+4z3VAgMBAAECggEATjtUroE76hFrMrQUaV7mblG750fvLqfzvL7bYWXgj2NK4yGRKC7t1QRVK+Cua3jkXhLX+xiw0ss6e3HvrQ9GzAkMaFe3iJAK2QEmosFWnpbs7FEISk5p/vXrE95iadeWxVy4FyFO1fXv54RrE6NmslMeVvdWqzYNnGp1O0SJVdd2+0YRsBdvnaLfd+z7qwZ0MiSGute0ngxSbyvcv8vpc9MCXt3EVWG2PtePinJ0umqp/L8C2+DvQ9Afw+0B7wKLaRcRXiZuR2MhekQW9jBtEqnQ9EWQ3gz32reF71lZRWB3dBk26Sn5TBLQpj5maaHXd8FuDhh7hrDv6ygyi6kERQKBgQDMl1eu697fxYBWiWh8zYLAEwHQYBsTBIN+skY9piSDP4ymMxGQR0gLR97vsukcnjjSvhHcKbnEFCnIWnNW8Q/QfKG7q0D7vEd0t3xLzii+5mkJRgwdIIfBnm5A7Z7MTY7RXeac5bHHeBRGVr8joVaOxzadK+VrWOMBthjQoBsbtwKBgQC0O5XOiSs/LIsHri1rtic2Gkt3ZOA/bk0VAXQJanA7RLH1R4Pbriy8oE7Bp7Qc7hteU+PlWZ9eRP/kl5malxxE8jXZDKVngwK/h6UpUoQO7Bhdsu9h0c1xaOH4Mk+j86h6XdNbmbGPzPROWXTx8PGKHOp5rMI0w+3V4btbirvK0wKBgFMSxNzXE7mDJZh91PqQLB432Y216Ghxdb9VPiY8Nyk//alipSgTeZJzDXgBCpy/Nk2ENhZSrwaKNWkQKTNNUOGUKcvdPta43b7r3kGdpXfWcSH92l9dXSJmZVhh5BALEgm7ei+NKQHYpUrmYFhkY5Q4Gdn5//71qb+BJFRaHofDAoGBALK2Qp1BNji2GkXLWA5t2AO1nw8MX6+F17j0b/LGL/MU8sCb4Qt7cbC35F/xwo7NKyYlWNyaOYRoNrSb/htjqwxSf0JxX7F8HsV74zFOONPzF1YRj3wXZPcHYMmSWZXgjTaIwoMZqw9D0xu9aFBe8aJZecacnxVjEfn6kqT6s6sxAoGAKXonjHIpyLI5sNzZN8zSLqKtA7sZ+BnS4YsAa3QKBra6F2/2LHtbrGYtZkgd79vQMYFXgvOWY2HrHdbX7qt6+I9rQaz2WG97pIEZqQ3oryF55eQr+GkcAh2G6Op+inBpN7jUpl8tW7IZM4Amg5KbP/+agrmu1840FdKVqlNF4hg=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAowPMCsSWbmTKD6S/27i+LN80qso1gSXHLB5kt0xxeXxSd+LXLqFD/YwC03CSXYpZMXlt4yN7EeLpSlfLDeCzEKswPGaD0iulmJEbOY8R/Sl4KuaXI5HUDrmVBIH21IMEG/miauwPu1PHwTM3QG3VkAVwnGw0VvmL8YO6QjYYCKewZVyRRSUfi462ssCoAjtiOyq9Q6mKvMTd5u3emjzxwyV90UmBokEjMQSNtQcN4iFswozhW31q6JlJ9LM6hZJH6fDj8vgsPm8SUlmAGILAF/zJwSSTxGZLVMvirSSKzv2/0De3yZsPev1oHuT1sh3lvsJPtH7ybP2AKUfvF7pHLwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "https://5806b580a9.goho.co/payed/notify";
    ;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";
    ;

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout="30m";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
